/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 21626
 * Date: 2024-09-26
 * Time: 17:48
 */
//写一个泛型，求泛型数组中的最大值
class Alg<T extends Comparable<T>> {        //上界为实现了Comparable接口的类型（Object就没有实现）,参数是能使用comparable接口的类
    public T findMax(T[] arry){
        T max = arry[0];
        for (int i = 1; i < arry.length; i++) {
            //if(max < arry[i]){    引用类型不能单纯用"<,>"比较
            if(max.compareTo(arry[i]) < 0){
                    max = arry[i];
                }
            }
        return max;
        }
}
class Alg2 {        //泛型方法
    public<T extends Comparable<T>> T findMax(T[] arry){
        T max = arry[0];
        for (int i = 1; i < arry.length; i++) {
            //if(max < arry[i]){    引用类型不能单纯用"<,>"比较
            if(max.compareTo(arry[i]) < 0){
                max = arry[i];
            }
        }
        return max;
    }
}
public class Test2 {
    public static void main(String[] args) {
        Integer[] arry = {1,2,3,4,5};
        Alg2 alg = new Alg2();
        Integer a = alg.<Integer>findMax(arry);
        System.out.println(a);
    }
    public static void main1(String[] args) {
        Integer[] arry = {1,2,3,4,5};
        Alg<Integer> alg = new Alg<Integer>();//Integer 实现了comparable接口
        Integer a = alg.findMax(arry);
        System.out.println(a);
    }
}
